package com.xzj.admin.boot.controller;

import com.xzj.admin.boot.domain.vo.SysMenuVO;
import com.xzj.common.id.gen.DefaultIdHelper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.boot.domain.entity.SysMenu;
import com.xzj.admin.boot.service.SysMenuService;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: SysMenuController
 * @description: 菜单;(sys_menu)表控制层
 * @author: XiaZunJun
 * @create: 2024-7-30
 */
@Tag(name = "菜单对象功能接口")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController{
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 通过ID查询单条数据
     *
     * @param menuId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{menuId}")
    public SysMenu queryById(@PathVariable(name = "menuId") long menuId){
        return sysMenuService.queryById(menuId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<SysMenu> pageQuery(@RequestBody PageReq<SysMenu> pageReq){
        return sysMenuService.pageQuery(pageReq);
    }

    @Operation(summary ="查询菜单树")
    @GetMapping("/menuTree")
    public List<SysMenuVO> menuTree(SysMenu sysMenu){
        return sysMenuService.menuTree(sysMenu);
    }
    /**
     * 新增数据
     *
     * @param sysMenu 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public SysMenu addOrUpdate(@RequestBody SysMenu sysMenu){
        if(ObjectUtil.isNotEmpty(sysMenu.getMenuId())){
            sysMenuService.update(sysMenu);
        }else{
            sysMenu.setMenuId(DefaultIdHelper.nextIdStr());
            sysMenuService.insert(sysMenu);
        }
        return sysMenu;
    }

    /**
     * 通过主键删除数据
     *
     * @param menuId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{menuId}")
    public boolean deleteById(@PathVariable(name = "menuId") long menuId){
        return sysMenuService.deleteById(menuId);
    }
}